import Vue from 'vue'
import Router from 'vue-router'
import home from '@/views/home'
import detail from '@/views/detail'
import audio from '@/views/home/audio'
import news from '@/views/home/news'
import data from '@/views/home/data'
import main from '@/views/home/main'

// const home = ()=>require.ensure([],(require)=>require('@/views/home'),"home")
// const audio = ()=>require.ensure([],(require)=>require('@/views/home/audio'),"audio")
// const news = ()=>require.ensure([],(require)=>require('@/views/home/news'),"news")
// const data = ()=>require.ensure([],(require)=>require('@/views/home/data'),"data")
// const main = ()=>require.ensure([],(require)=>require('@/views/home/main'),"main")
// const detail = ()=>require.ensure([],(require)=>require('@/views/detail'),"detail")

Vue.use(Router)

let router =  new Router({
    linkActiveClass:'active',
    routes: [
        {
            path:'/',
            redirect:'/home/news'
        },
        {
            path:'/home',
            component:home,
            redirect:'/home/audio',
            children:[
                {
                    path:'/home/audio',
                    component:audio
                },
                {
                    path:'/home/news',
                    component:news
                },
                {
                    path:'/home/main',
                    component:main
                },
                {
                    path:'/home/data',
                    component:data
                }
            ]
        },
        {
            path:'/detail/:id',
            component:detail,
            name:'detail',
            // 路由独享守卫
            // beforeEnter(to,from,next){
            //     let uid = localStorage.getItem('uid') ||'';
            //     if(uid){
            //         next()
            //     }else{
            //         next('/home')
            //     }
            // }
        }
    ]
})

//全局

let arr = ['detail']
router.beforeEach((to,from,next) => {
    if(arr.indexOf(to.name) != -1){
        let uid = localStorage.getItem('uid') ||'';
        if(uid){
            next()
        }else{
            next('/home')
        }
    }else{
        next()
    }
})

export default router